#include "StdAfx.h"
#include "EventHookerWraper.h"

CEventHookerWraper::CEventHookerWraper( VOID ) :
	m_hModule(					NULL ),
	m_fpInstallHook(			NULL ),
	m_fpUnInstallHook(			NULL ),
	m_fpUnTrayAll(				NULL ),
	m_fpGetTrayMessageID(		NULL ),
	m_fpGetMinimizedWinodwList(	NULL ),
	m_fpUnTrayMe(				NULL ),
	m_nTrayMessageID(			NULL ),
	m_hTrayWnd(					NULL )
{
}

CEventHookerWraper::~CEventHookerWraper( VOID )
{
	this->CleanUp();
}

BOOL CEventHookerWraper::Initialize( VOID )
{
	this->CleanUp();

	HMODULE hModule = this->GetModuleHandle();
	if( !hModule )
		return FALSE;

	this->m_fpInstallHook				= ( fpInstallHook				)::GetProcAddress( hModule, "InstallWinProcHook"		);
	this->m_fpMouseHook					= ( fpMouseHook					)::GetProcAddress( hModule, "InstallMouseHook"			);
	this->m_fpGetTrayMessageID			= ( fpGetTrayMessageID			)::GetProcAddress( hModule, "GetTrayMessageID"			);
	this->m_fpUnTrayAll					= ( fpUnTrayAll					)::GetProcAddress( hModule, "UnTrayAll"					);
	this->m_fpUnInstallHook				= ( fpUnInstallHook				)::GetProcAddress( hModule, "UnInstallWinProcHook"		);
	this->m_fpMouseHook					= ( fpUnInstallHook				)::GetProcAddress( hModule, "UnInstallMouseHook"		);
	this->m_fpGetMinimizedWinodwList	= ( fpGetMinimizedWinodwList	)::GetProcAddress( hModule, "GetMinimizedWinodwList"	);
	this->m_fpUnTrayMe					= ( fpUnTrayMe					)::GetProcAddress( hModule, "UnTrayMe"					);

	if(	!this->m_fpInstallHook				||
		!this->m_fpMouseHook				||
		!this->m_fpGetTrayMessageID			||
		!this->m_fpUnTrayAll				||
		!this->m_fpUnInstallHook			||
		!this->m_fpMouseHook				||
		!this->m_fpGetMinimizedWinodwList	||
		!this->m_fpUnTrayMe )
		return FALSE;

	if( !this->InstallHook() ||
		this->MouseHook() )
		return FALSE;

	this->m_nTrayMessageID = this->GetTrayMessageID();

	return this->m_nTrayMessageID ? TRUE : FALSE;
}

VOID CEventHookerWraper::CleanUp( VOID )
{
	if( this->m_hModule )
	{
		this->UnTrayAll();
		this->UnInstallHook();
		this->UnInstallHook();
		
		::FreeLibrary( this->m_hModule );
		this->m_hModule = NULL;
	}
}

HMODULE CEventHookerWraper::GetModuleHandle( VOID )
{
	if( this->m_hModule )
		return this->m_hModule;

	this->m_hModule = LoadLibrary( EVENT_HOOKER_DLL_NAME );
	return this->m_hModule;
}

BOOL CEventHookerWraper::InstallHook( VOID )
{
	return this->m_fpInstallHook();
}

BOOL CEventHookerWraper::MouseHook( VOID )
{
	return this->m_fpMouseHook();
}

BOOL CEventHookerWraper::UnInstallHook( VOID )
{
	return this->m_fpUnInstallHook();
}

VOID CEventHookerWraper::UnTrayAll( VOID )
{
	this->m_fpUnTrayAll();
}

UINT CEventHookerWraper::GetTrayMessageID( VOID )
{
	return this->m_fpGetTrayMessageID();
}

VOID CEventHookerWraper::GetMinimizedWindowsList( TCHAR szParam[ ARRAY_SIZE ][ 100 ] )
{
	this->m_fpGetMinimizedWinodwList( szParam );
}

BOOL CEventHookerWraper::UnTrayMe( TCHAR * pszTitle )
{
	return this->m_fpUnTrayMe( pszTitle );
}

BOOL CEventHookerWraper::InitializeTray( HWND hWnd, HICON hIcon, TCHAR * pszToolTipMessage )
{
	this->m_hTrayWnd = hWnd;
	
	NOTIFYICONDATA niData;
	niData.cbSize			= sizeof( NOTIFYICONDATA );
	niData.hWnd				= this->m_hTrayWnd;
	niData.uID				= 1;
	niData.uCallbackMessage	= WM_TRAY_ICON_NOTIFY_MESSAGE;
	niData.hIcon			= hIcon;
	niData.uFlags			= NIF_MESSAGE | NIF_ICON | NIF_TIP;
	
	::ZeroMemory( niData.szTip, 128 );
	if( pszToolTipMessage )
		::StringCchCopy( niData.szTip, 127, pszToolTipMessage );

	::Shell_NotifyIcon( NIM_ADD, &niData );

	return ::RegisterHotKey( this->m_hTrayWnd, HOT_KEY_ID, MOD_CONTROL | MOD_SHIFT, VK_DOWN );
}

BOOL CEventHookerWraper::UnInitializeTray( VOID )
{
	if( m_hTrayWnd )
	{
		NOTIFYICONDATA niData;
		niData.cbSize	= sizeof( NOTIFYICONDATA );
		niData.hWnd		= this->m_hTrayWnd;
		niData.uID		= 1;

		::Shell_NotifyIcon( NIM_DELETE, &niData );
		
		return ::UnregisterHotKey( m_hTrayWnd, HOT_KEY_ID );
	}

	return FALSE;
}